import { each, wrapBehavior } from '@antv/util';

export default class PluginBase {

  constructor(cfgs) {
    this._cfgs = Object.assign(this.getDefaultCfgs(), cfgs);
    this._events = {};
    this.destroyed = false;
  }

  getDefaultCfgs() { return {}; };
  getEvents() { return {}; };

  get(key) { return this._cfgs[key]; };
  set(key, val) { this._cfgs[key] = val; };

  initPlugin(graph) {
    this.set('graph', graph);
    var events = this.getEvents();
    var bindEvents = {};
    each(events, (v, k) => {
      var event = wrapBehavior(this, v);
      bindEvents[k] = event;
      graph.on(k, event);
    });
    this._events = bindEvents;
    this.init();
  };

  destroyPlugin() {
    this.destroy();
    var graph = this.get('graph');
    var events = this._events;
    each(events, (a, k) => graph.off(a, k));
    this._events = null;
    this._cfgs = null;
    this.destroyed = true;
  };
  destroy() { };
}